package com.ayic.garbagerecycle.controller;


import com.ayic.garbagerecycle.mapper.StationMapper;
import com.ayic.garbagerecycle.pojo.PageResult;
import com.ayic.garbagerecycle.pojo.Result;
import com.ayic.garbagerecycle.pojo.Station;
import com.ayic.garbagerecycle.service.AnalysisService;
import com.ayic.garbagerecycle.service.StationService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RequestMapping("/station")
@RestController
public class StationController {

    @Resource
    private StationService stationService;
    @Resource
    private AnalysisService analysisService;
    @Autowired
    private StationMapper stationMapper;

    @GetMapping("/query")
    public Result query(@RequestParam int page, @RequestParam int pageSize) {
        // 计算分页起始位置
        int offset = (page - 1) * pageSize;
        // 查询数据
        List<Station> stations = stationService.queryList(offset, pageSize);
        // 获取总记录数
        int total = stationService.getTotalCount();
        // 封装分页结果
        PageResult<Station> pageResult = new PageResult<>(stations, total, page, pageSize);
        // 返回封装好的响应结果
        return Result.success(pageResult);
    }

    //查询站点信息
    @GetMapping("/{stationId}")
    public Result queryStationInfo(@PathVariable Integer stationId) {
        Station station = stationService.getStationById(stationId);
        if (station == null)
            return Result.error("站点不存在");
        return Result.success(station,"成功获取站点信息");
    }

    // 更新站点数据状态
    @PutMapping("/{stationId}")
    public Result updateStationCapacity(@PathVariable Integer stationId) {
        return stationService.updateCurrentCapacity(stationId);
    }

    // 新增对站点容量的预测
    @GetMapping("/predict/{stationId}")
    public Result predictStationCapacity(@PathVariable Integer stationId) {
        return Result.success(analysisService.generateStationReport(stationId));
    }

    @GetMapping("/clean/{stationId}")
    public Result cleanStationCapacity(@PathVariable Integer stationId) {
        Station station = stationService.getStationById(stationId);
        if (station.getCurrentCapacity()>=80)
        {
            stationMapper.cleanStationCapacity(stationId);
            return Result.success("站点清理成功");
        }
        else if (station.getCurrentCapacity()>0){
            return Result.success("站点容量未超出上限，当前容量为"+station.getCurrentCapacity());
        }
        else {
            return Result.error("清理站点发送错误，请联系管理员！");
        }
    }

    /**
     * 获取用户负责的高容量站点列表
     * @param username 用户名
     * @return 高容量站点列表
     */
    @GetMapping("/high-capacity/{username}")
    public Result getHighCapacityStations(@PathVariable String username) {
        List<Station> stations = stationService.getHighCapacityStationsByUser(username);
        return Result.success(stations, "成功获取高容量站点列表");
    }

}
